﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SPlusres.Business;

namespace SPlusres.Business.Courseware
{
    public static class CoursewareExtension
    {
        /// <summary>
        /// To small model
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static List<CoursewareData.SmallCoursewareModel> ToSmall(this IQueryable<Data.Courseware> query)
        {
            return query.Select(m => new CoursewareData.SmallCoursewareModel()
            {
                ID = m.ID,
                Title = m.Title,
                Createtime = m.Createtime
            }).ToList();
        }
    }

    public class CoursewareData
    {
        public class SmallCoursewareModel
        {
            /// <summary>
            /// ID
            /// </summary>
            public int ID { get; set; }

            /// <summary>
            /// Title
            /// </summary>
            public string Title { get; set; }

            /// <summary>
            /// Click
            /// </summary>
            public int Click { get; set; }

            /// <summary>
            /// Create time
            /// </summary>
            public DateTime Createtime { get; set; }
        }

        /// <summary>
        /// Subject List
        /// </summary>
        /// <returns>List</returns>
        public static List<CoursewareData.SmallCoursewareModel> Hot(int? stage = null, int? subject = null)
        {
            using (var data = new Data.SPlusresDataContext(SystemSetting.ConnectionString))
            {
                var query = data.Courseware.AsQueryable();

                if (stage.HasValue)
                    query = query.Where(m => m.Stage == stage);

                if (subject.HasValue)
                    query = query.Where(m => m.Subject == subject);

                query = query.OrderByDescending(m => m.Hot).ThenByDescending(m => m.ID).Take(5);

                return query.ToSmall();
            }
        }

        /// <summary>
        /// Subject List
        /// </summary>
        /// <returns>List</returns>
        public static List<CoursewareData.SmallCoursewareModel> New(int? stage = null, int? subject = null)
        {
            using (var data = new Data.SPlusresDataContext(SystemSetting.ConnectionString))
            {
                var query = data.Courseware.AsQueryable();

                if (stage.HasValue)
                    query = query.Where(m => m.Stage == stage);

                if (subject.HasValue)
                    query = query.Where(m => m.Subject == subject);

                query = query.OrderByDescending(m => m.ID).Take(5);

                return query.ToSmall();
            }
        }

        /// <summary>
        /// Subject List
        /// </summary>
        /// <returns>List</returns>
        public static List<CoursewareData.SmallCoursewareModel> Click(int? stage = null, int? subject = null)
        {
            using (var data = new Data.SPlusresDataContext(SystemSetting.ConnectionString))
            {
                var query = data.Courseware.AsQueryable();

                if (stage.HasValue)
                    query = query.Where(m => m.Stage == stage);

                if (subject.HasValue)
                    query = query.Where(m => m.Subject == subject);

                query = query.OrderByDescending(m => m.Click).ThenByDescending(m => m.ID).Take(5);

                return query.ToSmall();
            }
        }
    }
}
